//
//  LWRadioButton.swift
//  shop
//
//  Created by 刘伟 on 2017/4/4.
//
//

protocol LWRadioButtonDelegate: class {
    func didSelectedRadioButton(radio: LWRadioButton, groupId: String)
}

import UIKit

class LWRadioButton: UIButton {
    var groupId: String!
    var checked: Bool! = false {
        didSet {
            if checked == true {
                self.backgroundColor = UIColor.primary
                self.setTitleColor(UIColor.white, for: .normal)
                uncheckOtherRadios()
            } else {
                self.backgroundColor = UIColor.normal
                self.setTitleColor(UIColor.black, for: .normal)
            }
        }
    }
    weak var delegate: LWRadioButtonDelegate?
    
    static var groupRidioDic = [String: [LWRadioButton]]()
    
    init(frame: CGRect, groupId: String) {
        self.groupId = groupId
        super.init(frame: frame)
        self.frame = frame
        print(frame)
        self.isExclusiveTouch = true
        self.layer.cornerRadius = 2
        self.layer.borderColor = UIColor.black.cgColor
        self.layer.borderWidth = 1
        self.backgroundColor = UIColor.normal
        self.setTitleColor(UIColor.black, for: .normal)
        self.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        self.addTarget(self, action: #selector(radioBtnChecked), for: .touchUpInside)
        addGroup()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate func addGroup() {
        var radioArray = LWRadioButton.groupRidioDic[groupId] ?? [LWRadioButton]()
        radioArray.append(self)
        LWRadioButton.groupRidioDic[groupId] = radioArray
    }
    
    fileprivate func remvoeFromGroup() {
        if var radioArray = LWRadioButton.groupRidioDic[groupId] {
            for i in 0 ..< radioArray.count {
                if radioArray[i].isEqual(self) {
                    radioArray.remove(at: i)
                }
            }
            LWRadioButton.groupRidioDic[groupId] = radioArray
        }
        
    }
    
    fileprivate func uncheckOtherRadios() {
        let radioArray = LWRadioButton.groupRidioDic[groupId] ?? [LWRadioButton]()
        if radioArray.count > 0 {
            for radio in radioArray {
                if !radio.isEqual(self) {
                    radio.checked = false
                }
            }
        }
    }
    
    func setChecked(checked: Bool) {
        if self.checked == checked {
            return
        }
        self.checked = checked
        if self.checked == true {
            delegate?.didSelectedRadioButton(radio: self, groupId: groupId)
        }
    }
    
    func radioBtnChecked() {
        if self.checked == true {
            return
        }
        self.checked = !self.checked
        if self.checked == true {
            delegate?.didSelectedRadioButton(radio: self, groupId: groupId)
        }
    }
    
    deinit {
        print("详情页消失了")
    }
    
}
